﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CdcSoftware.Pivotal.Applications.Core.Client;
using CdcSoftware.Pivotal.Engine.Client.ClientTasks.Attributes;
using CdcSoftware.Pivotal.Engine.Client.Services.Interfaces;
using System.Data;
using CdcSoftware.Pivotal.Applications.Core.Common;
using System.Windows.Forms;
using CdcSoftware.Pivotal.Engine;
using CdcSoftware.Pivotal.Engine.UI.Forms;
using CdcSoftware.Iaf.DomainObjects;

namespace Pcm.Retail.Client.FormTasks.CommandHandlers
{
    public class RE_Promotion_SelectionListCommandHandlers : CommandClientTask
    {
        [ClientTaskCommand]
        public virtual void OnClickSelectPromotion()
        {
            try
            {
                IFormView formView = (IFormView)ClientContext.ClientAPIService.GetActiveContentObject();
                CdcSoftware.Pivotal.Engine.UI.Forms.PivotalClientFormControl formControl = (CdcSoftware.Pivotal.Engine.UI.Forms.PivotalClientFormControl)formView.FormControl;

                PivotalSearchResultsGrid pvtSearchResultsGrid = formControl.GetControlByName("promotionSelectionList") as PivotalSearchResultsGrid;
                IEnumerable<CdcSoftware.Iaf.DomainObjects.IDomainObject> selectedRows = pvtSearchResultsGrid.ResultsViewControl.SelectedItems;

                if (selectedRows.Count() == 0)
                    PivotalMessageBox.Show("Please select Promotion(s) to Apply.", MessageBoxButtons.OK, MessageBoxIcon.Information);
                else
                {
                    Globals.SelectedPromotions = new List<object>(selectedRows.Count());
                    foreach (DomainObject item in selectedRows)
                    {                        
                        Globals.SelectedPromotions.Add(item.GetAttribute("Promotion_Id").Value);
                    }
                    this.FormView.CloseForm(true);
                }                
            }
            catch (Exception ex)
            {
                Globals.HandleException(ex, true);
            }
        }
    }
}
